package com.ryan.demo.xstream.utils.json;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;

import com.ryan.demo.xstream.model.json.Student;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;

public class JsonParser {
    
    private XStream xstream = null;
    private ObjectOutputStream  out = null;
    private ObjectInputStream in = null;
    
    public JsonParser(){
    	init();
    }
    
    /**
     * <b>function:</b>初始化资源准备
     * @author hoojo
     * @createDate Nov 27, 2010 12:16:28 PM
     */
    public void init() {
        try {
            xstream = new XStream();
            //xstream = new XStream(new DomDriver()); // 需要xpp3 jar
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * <b>function:</b>释放对象资源
     * @author hoojo
     * @createDate Nov 27, 2010 12:16:38 PM
     */
    public void destory(Student bean) {
        xstream = null;
        bean = null;
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.gc();
    }
    
    /**
     * <b>function:</b>XStream结合JettisonMappedXmlDriver驱动，转换Java对象到JSON
     * 需要添加jettison jar
     * @author hoojo
     * @createDate Nov 27, 2010 1:23:18 PM
     */
    public String entity2Json(Student bean) {
    	System.out.println("=======JettisonMappedXmlDriver===JavaObject >>>> JaonString=========");
        xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("student", Student.class);
        return xstream.toXML(bean);
    }
    
    public String list2Json(List list) {
        JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
        xstream = new XStream(driver);
        return xstream.toXML(list);
    }
    
    public String map2Json(Map map) {
        System.out.println("======JsonHierarchicalStreamDriver====JavaObject >>>> JaonString=========");
        JsonHierarchicalStreamDriver driver = new JsonHierarchicalStreamDriver();
        xstream = new XStream(driver);
        //xstream = new XStream(new JettisonMappedXmlDriver());//转换错误
        //xstream.setMode(XStream.NO_REFERENCES);
        
//        xstream.alias("student", Student.class);
        
        /*
        List<Student> list = new ArrayList<Student>();
        list.add(bean);//add
        
        bean = new Student();
        bean.setAddress("china");
        bean.setEmail("tom@125.com");
        bean.setId(2);
        bean.setName("tom");
        Birthday day = new Birthday("2010-11-22");
        bean.setBirthday(day);
        list.add(bean);//add
        
        bean = new Student();
        bean.setName("jack");
        list.add(bean);//add
        */
        return xstream.toXML(map);
        /*
        //System.out.println("========JsonHierarchicalStreamDriver==删除根节点=========");
        //删除根节点
        xstream = new XStream(new JsonHierarchicalStreamDriver() {
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new JsonWriter(out, JsonWriter.DROP_ROOT_MODE);
            }
        });
        xstream.alias("student", Student.class);
        System.out.println(xstream.toXML(list));
        */
    }

}
